<?php
namespace Index\Controller;
use Think\Controller;
class RegisterController extends Controller {
    public function Index(){
    	layout(false);
    	if(IS_POST){
    	$username =I('post.username');
    	$password =md5(I('post.password'));
    	$verify =I('post.verify');
    	$sex =I('post.sex');
    	$jobs =I('post.jobs');
    	$introduce =I('post.signature');
    	if ($this->check_verify($verify)) {

    		// 链接数据库
    		$data =M();
    		// 判断注册用户是否已存在
    		$sql = "select username from user where username='{$username}'";
    		$rist=$data->query($sql);
    		// var_dump($data);
    		if ($rist) {
    			$this->error('用户已存在，请重新输入！');
    		}
   	$sql ="insert into user (username,password,sex,introduce) value('$username','$password','$sex','$introduce')";
	    	$rist=$data->execute($sql);
	    	$this->success('注册成功！',U('index/Login/index'),3);
    	// var_dump($rist);
    	}else{
    		$this->error('验证码错误，请重新输入');
    	}


    	}else{
    		// 展示模版
    		 $this->display('register');	
    	}
    }

    public function code(){
    	$config =    array(
    		// 'imageW'      =>    200,
	        'fontSize'    =>    22,    // 验证码字体大小
	        'length'      =>    4,     // 验证码位数
	        'useNoise'    =>    false, // 关闭验证码杂点
	    );
	    $Verify =     new \Think\Verify($config);
	    $Verify->entry();
    }

    function check_verify($code, $id = ''){
	    $verify = new \Think\Verify();
	    return $verify->check($code, $id);
	}
}